﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Stauffware.Common.Core
{
	/// <summary>
	/// Parser for decimal values (System.Decimal).
	/// </summary>
	public class DecimalParser : NumberParser<decimal, DecimalParse>
	{
		/// <summary>
		/// Constructs a parser that uses DefaultParseOptions.
		/// </summary>
		public DecimalParser ( ) : base(DefaultParseOptions, null) { }

		/// <summary>
		/// Constructs a parser.
		/// </summary>
		/// <param name="options">parsing options</param>
		public DecimalParser (NumberParseOptions options) : base(options, null) { }

		/// <summary>
		/// Constructs a parser that uses DefaultParseOptions.
		/// </summary>
		/// <param name="ifp">formatting info, or null</param>
		public DecimalParser (IFormatProvider ifp) : base(NumberParseOptions.None, ifp) { }

		/// <summary>
		/// Constructs a parser.
		/// </summary>
		/// <param name="options">parsing options</param>
		/// <param name="ifp">formatting info, or null</param>
		public DecimalParser (NumberParseOptions options, IFormatProvider ifp) : base(options, ifp) { }

		//-----------------------------------------------------------------------------
		///// <summary>
		///// Attempts to parse a string for a value of type decimal (System.Decimal).
		///// </summary>
		///// <param name="input">the string to be parsed</param>
		///// <returns>the result of the parse attempt (never null)</returns>
		
		//public DecimalParse Parse (string input)
		//{
		//    return (DecimalParse) this._Parse(input);
		//}

		//-----------------------------------------------------------------------------
		/// <summary>
		/// Assembles selected tokens from this.Lexer into a normalized number 
		/// string (no digit separators or whitespace).
		/// </summary>
		/// <param name="number">receives the normalized number</param>
		/// <param name="error">receives an error string (if false is returned)</param>
		/// <returns>true on success, false on error</returns>

		protected override bool AssembleNormalizedNumber (out string number, out string error)
		{
			error = null;
			number = this.Lexer.DigitsLeft;

			if (this.Lexer.IsNegative)
				number = "-" + number;

			if (!string.IsNullOrEmpty(this.Lexer.DigitsRight))
				number += "." + this.Lexer.DigitsRight;

			if (!string.IsNullOrEmpty(this.Lexer.ExponentDigits))
				number += "E" + this.Lexer.ExponentDigits;

			return true;
		}

		//-----------------------------------------------------------------------------

		/// <summary>
		/// Gets the maximum number of digits (base 10) that a number can have.
		/// </summary>
		protected override int MaxDigits { get { return 29; } }

		/// <summary>
		/// Gets the maximum number of digits that can appear in an exponent.
		/// </summary>
		protected override int MaxExponentDigits { get { return 2; } }

	//	protected override NumberParse<decimal> CreateParseObject ( )
	//	{ return new DecimalParse(); }

		/// Attempts a parse of a normalized integer string.  (see IntegerParser.TryParse)
		protected override bool TryParse (string str, out decimal value)
		{ return decimal.TryParse(str, out value); }
	}

	//#################################################################################
	/// <summary>
	/// Encapsulates the result of trying to parse a string into a Decimal value.
	/// </summary>

	public class DecimalParse : NumberParse<decimal>
	{


	}

	//#################################################################################
}
